package com.example.ccj.b.c.custom.tree;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.ccj.R;
import com.example.ccj.u.ImageUtils;
import com.example.ccj.u.SizeUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.ThreadUtils;
import com.example.ccj.u.ThreadUtilsListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 树结构检索
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/11/22 0:37
 */
public class TreeSearchAdapter extends RecyclerView.Adapter<TreeSearchAdapter.ViewHolder>{
    private final List<TreeMode> treeModeList;
    public Map<String,TreeMode> selectedTree;
    private final Map<Integer,TreeMode> muLuMap;
    private final ISelectTreeListener selectTreeListener;

    static RequestOptions requestOptions = RequestOptions
            .bitmapTransform(new RoundedCorners(SizeUtils.dp2px(10)));

    /**
     *
     * @param treeModeList 数据
     * @param selectedTree 默认选中的数据
     * @param selectTreeListener 回调
     */
    public TreeSearchAdapter(List<TreeMode> treeModeList, Map<String,TreeMode> selectedTree, ISelectTreeListener selectTreeListener){
        this.treeModeList = treeModeList;
        this.selectedTree = selectedTree;
        this.selectTreeListener = selectTreeListener;

        if (this.selectedTree == null) this.selectedTree = new HashMap<>();
        muLuMap = new HashMap<>();
        for (TreeMode treeMode : treeModeList) {
            if (selectedTree != null && selectedTree.containsKey(treeMode.dataId)) treeMode.isChecked = true;
            if (treeMode.type == TreeMode.DATA_TYPE_MU_LU) muLuMap.put(treeMode.id,treeMode);
        }
    }

    private int iconMuLu = 0;                               //目录默认图片资源ID
    private int iconData = 0;                               //数据默认图片资源ID


    //设置目录默认图片资源id
    public void setMuLuDefaultImageResource(int imageResourceId){
        this.iconMuLu = imageResourceId;
    }
    //设置数据默认图片资源id
    public void setDataDefaultImageResource(int imageResourceId){
        this.iconData = imageResourceId;
    }

    @NonNull
    @Override
    public TreeSearchAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new TreeSearchAdapter.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_tree, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull TreeSearchAdapter.ViewHolder holder, int position) {
        TreeMode treeMode = treeModeList.get(position);
        holder.name.setText(treeMode.name);
        //是否显示
        if (treeMode.isVisibility) {
            holder.lL.setVisibility(View.VISIBLE);
            holder.jianTou.setVisibility(View.GONE);

            //设置图标
            if (treeMode.type == TreeMode.DATA_TYPE_MU_LU) {
                if (!StringUtils.isEmpty(treeMode.icon)) {
                    holder.icon.setVisibility(View.VISIBLE);
                    ImageUtils.loadImage(holder.icon.getContext(),treeMode.icon,holder.icon,requestOptions);
                } else if (iconMuLu > 0) {
                    holder.icon.setVisibility(View.VISIBLE);
                    ImageUtils.loadImage(holder.icon.getContext(),iconMuLu,holder.icon,requestOptions);
                } else holder.icon.setVisibility(View.GONE);
            } else if (treeMode.type == TreeMode.DATA_TYPE_DATA) {
                if (!StringUtils.isEmpty(treeMode.icon)) {
                    holder.icon.setVisibility(View.VISIBLE);
                    ImageUtils.loadImage(holder.icon.getContext(),treeMode.icon,holder.icon,requestOptions);
                } else if (iconData > 0) {
                    holder.icon.setVisibility(View.VISIBLE);
                    ImageUtils.loadImage(holder.icon.getContext(),iconData,holder.icon,requestOptions);
                } else holder.icon.setVisibility(View.GONE);
            }
        } else {
            holder.lL.setVisibility(View.GONE);
        }

        //是否选中处理
        //传入的数据中是否存在选中的
        holder.select.setOnCheckedChangeListener(null);
        holder.select.setChecked(treeMode.isChecked);
        holder.select.setOnCheckedChangeListener((buttonView, isChecked) -> {
            //选中处理
            if (isChecked) xuanZhong(treeMode,holder);
            else cancelSelect(treeMode,holder);
        });
    }

    /**
     * 取消选中处理
     * @param treeMode 1
     */
    private void cancelSelect(TreeMode treeMode, TreeSearchAdapter.ViewHolder holder){
        treeMode.isChecked = false;
        if (selectTreeListener != null) selectTreeListener.delete(treeMode);
    }

    /**
     * 选中处理
     * @param treeMode 选中的节点
     * @param holder 用来主线程更新UI
     */
    private void xuanZhong(TreeMode treeMode, TreeSearchAdapter.ViewHolder holder){
        treeMode.isChecked = true;
        if (selectTreeListener != null) selectTreeListener.select(treeMode);
    }


    @Override
    public int getItemCount() {
        return treeModeList == null ? 0 : treeModeList.size();
    }


    @Override
    public int getItemViewType(int position) {
        return position;
    }

    //
    public static class ViewHolder extends RecyclerView.ViewHolder{
        private final TextView name;
        private final ImageView icon,jianTou;
        private final LinearLayout lL;
        private final CheckBox select;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            lL = itemView.findViewById(R.id.adapter_tree_ll);
            icon = itemView.findViewById(R.id.adapter_tree_icon);
            jianTou = itemView.findViewById(R.id.adapter_tree_jian_tou);
            name = itemView.findViewById(R.id.adapter_tree_text);
            select = itemView.findViewById(R.id.adapter_tree_select);
        }
    }



}
